package view
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import models.ModelLocator;
	
	public class SubmitDetail extends Sprite
	{
		private var txt:TextField;
		private var btn:ImageBtn;
		private var alertTxt:TextField;
		private var model:ModelLocator = ModelLocator.getInstance();
		public function SubmitDetail()
		{
			txt = new TextField();
			txt.width = 124;
			txt.height = 136;
			txt.multiline = true;
			txt.wordWrap = true;
			txt.selectable = false;
			txt.htmlText = "<font color='#6c5939' size='15'>请直接在右侧编辑区域拖动、旋转照片，确定构图后保存，然后点击下一步，开始计价与支付</font>"
			this.addChild(txt);
			txt.x = 10;
			txt.y = 10;
			
			alertTxt = new TextField();
			alertTxt.width = 124;
			alertTxt.height = 50;
			alertTxt.multiline = true;
			alertTxt.wordWrap = true;
			alertTxt.selectable = false;
			//alertTxt.htmlText = "<font color='#6c5939' size='15'>请直接在右侧编辑区域拖动、旋转照片，确定构图后保存，然后点击下一步，开始计价与支付</font>"
			this.addChild(alertTxt);
			alertTxt.x = 10;
			alertTxt.y = 220;
			
			btn = new ImageBtn("保存");
			this.addChild(btn);
			btn.y = 169;
			btn.x = 24;
			btn.addEventListener(MouseEvent.CLICK,click);
			
		}
		public function click(event:MouseEvent):void{
			if(model.size == null){
				alertTxt.htmlText = "<font color='#6c5939' size='15'>还没有选择尺寸</font>";
				return;
			}
			/*if(model.selectData == null){
				alertTxt.htmlText = "<font color='#6c5939' size='15'>还没有选择画框</font>";
				return;
			}*/
			alertTxt.htmlText = "";
			SubmitAlert.getInstance().add();
			SubmitAlert.getInstance().fun = sendServer;
		}
		private function sendServer():void{
			var byte:ByteArray = EditFrame.getInstance().getByteArray();
			var urlloader:URLLoader = new URLLoader();
			var sendID:String;
			if(model.selectData){
				sendID = model.selectData.id
			}else{
				sendID = "-1";
			}
			var url:String = ModelLocator.getInstance().config.submit + "?size=" + model.size + "&type=" + sendID;
			var urlreq:URLRequest = new URLRequest(url)
			urlreq.data = byte;
			urlreq.method = URLRequestMethod.POST;  
			urlreq.contentType = "application/octet-stream";  
			urlloader.addEventListener(Event.COMPLETE,com);
			urlloader.addEventListener(IOErrorEvent.IO_ERROR,ioError);
			urlloader.load(urlreq);
			alertTxt.htmlText = "<font color='#6c5939' size='15'>图片上传中……</font>";
		}
		private function com(evnet:Event):void{
			trace("上传成功！！")
			alertTxt.htmlText = "<font color='#6c5939' size='15'>上传成功</font>";
			ExternalInterface.call("submit",true);
		}
		private function ioError(event:IOErrorEvent):void{
			trace("ioerror");
			alertTxt.htmlText = "<font color='#6c5939' size='15'>上传失败</font>";
			ExternalInterface.call("submit",false);
		}
	}
}